package fc;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import fc.entity.OaEntity;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.upgradetool.httpclient.org.apache.http.client.HttpClient;
import weaver.upgradetool.httpclient.org.apache.http.client.entity.UrlEncodedFormEntity;
import weaver.upgradetool.httpclient.org.apache.http.client.methods.HttpPost;
import weaver.upgradetool.httpclient.org.apache.http.impl.client.DefaultHttpClient;
import weaver.upgradetool.httpcore.org.apache.http.HttpResponse;
import weaver.upgradetool.httpcore.org.apache.http.NameValuePair;
import weaver.upgradetool.httpcore.org.apache.http.message.BasicNameValuePair;
import weaver.upgradetool.httpcore.org.apache.http.util.EntityUtils;
import weaver.upgradetool.wscheck.Util;

import java.util.ArrayList;
import java.util.List;

public class ZH_HJ_DAORU1 implements Action{

	@Override
	public String execute(RequestInfo info) {
		String API_NAME="ZH_HJ_DAORU";

		BaseBean log = new BaseBean();

		log.writeLog("接口"+API_NAME+"开始调用-----------------------------------");

		OaEntity oe=new OaEntity();

		String requestid = info.getRequestid();
		String tablename = info.getRequestManager().getBillTableName();
		oe.setREQUESTID(requestid);

		String sql=


"select  供应商	CUS_NO,生效日期 SXRQ,失效日期 JZRQ,核价单别 TYPENO,税种 TAX_NO,付款条件 FKTJ,币种 CUR_ID,交易条件 JYTJ,料号 LJBH ,单价 UP,备注 REM	,涨价原因 CAUSE, "+
"单据日期 WLXQSJ, 	申请部门 DEPARTMENTID,	申请人 USERID,workflowid judge3 ,姓名 Judge2,nno judge4  from ( "+
"select*,row_number() over (partition by mainid,供应商,核价单别,生效日期,失效日期,hbxh order by id ) nno  from ( "+
"select  o1.id,o1.mainid,o1.gys 供应商,o1.sxrq1 生效日期,o1.sxrq2 失效日期,o1.hjdb 核价单别, "+
"o1.sz 税种,o1.fktj 付款条件,o1.bz 币种,o1.jytj 交易条件,o1.lh 料号,o1.dj 单价, "+
"o1.remark 备注,o1.zjyy 涨价原因,o1.hbxh, "+
"o2.djrq 单据日期,o2.sqbm 申请部门,o2.sqr 申请人,o2.fqlcid workflowid,o4.lastname 姓名  "+
"from  "+tablename+"_dt1  o1,"+tablename+" o2 ,workflow_requestbase o3 ,HrmResource o4 "+
"where o1.mainid=o2.id  and o2.requestid=o3.requestid  and o3.creater=o4.id "+
"and o2.requestid="+requestid+
")o "+
")oo where 1=1 order by nno,id";

		List<OaEntity> dts=new ArrayList<OaEntity>();

		RecordSet rs1 = new RecordSet();
		rs1.executeSql(sql);
		while(rs1.next()){
 			OaEntity dt1=new OaEntity();

			dt1.setCUS_NO(Util.null2String(rs1.getString("CUS_NO")));
			dt1.setSXRQ(Util.null2String(rs1.getString("SXRQ")));
			dt1.setJZRQ(Util.null2String(rs1.getString("JZRQ")));
			dt1.setTYPENO(Util.null2String(rs1.getString("TYPENO")));
			dt1.setTAX_NO(Util.null2String(rs1.getString("TAX_NO")));

			dt1.setFKTJ(Util.null2String(rs1.getString("FKTJ")));
			dt1.setCUR_ID(Util.null2String(rs1.getString("CUR_ID")));
			dt1.setJYTJ(Util.null2String(rs1.getString("JYTJ")));
			dt1.setLJBH(Util.null2String(rs1.getString("LJBH")));
			dt1.setUP(Util.null2String(rs1.getString("UP")));
			dt1.setREM(Util.null2String(rs1.getString("REM")));
			dt1.setCAUSE(Util.null2String(rs1.getString("CAUSE")));
			dt1.setWLXQSJ(Util.null2String(rs1.getString("WLXQSJ")));
			dt1.setDEPARTMENTID(Util.null2String(rs1.getString("DEPARTMENTID")));
			//dt1.setJUDGE1(Util.null2String(rs1.getString("JUDGE1")));
			dt1.setJUDGE2(Util.null2String(rs1.getString("JUDGE2")));//复用姓名
			dt1.setJUDGE3(Util.null2String(rs1.getString("JUDGE3")));//workflowid
			dt1.setJUDGE4(Util.null2String(rs1.getString("JUDGE4")));//用来拆分单子

			dts.add(dt1);
		}


		String testparam="";
		log.writeLog("接口"+API_NAME+"参数开始-----------------------------------"+oe.getREQUESTID());



		String judge_add="";

		for (int ii=0;ii<dts.size();ii++){
			if(judge_add.equals(dts.get(ii).getJUDGE4())||"".equals(judge_add)){
				dts.add(dts.get(ii));
			}else{
				//调用接口生成
				diaoyong(dts);
				dts=new ArrayList<OaEntity>();
				dts.add(dts.get(ii));
			}
			judge_add=dts.get(ii).getJUDGE4();
		}
		if(dts.size()>0){
			//调用接口生成
			diaoyong(dts);

		}


		return SUCCESS;


	}




	public static void diaoyong(List<OaEntity> dt1){
		BaseBean log = new BaseBean();

		GetToken tok=new GetToken();
		String token=(String) tok.testGetoken().get("token");
		log.writeLog("token:"+token);
		RSA rsa = new RSA(null, FcHttpCon.SPK);
		String encryptUserid = rsa.encryptBase64(dt1.get(0).getUSERID(), CharsetUtil.CHARSET_UTF_8, KeyType.PublicKey);
		log.writeLog("encryptUserid:"+encryptUserid);

		// 1. 创建HttpClient对象
		HttpClient httpClient = new DefaultHttpClient();

		// 2. 创建HttpPost对象，并设置请求的URL
		HttpPost httpPost = new HttpPost("http://oa.maxwell-gp.com:8090/api/workflow/paService/doCreateRequest");
		try {
			String maindata="["+
					"{\"fieldName\":\"djrq\",\"fieldValue\":\""+dt1.get(0).getWLXQSJ()+"\"},"+
					"{\"fieldName\":\"applydept\",\"fieldValue\":\""+dt1.get(0).getDEPARTMENTID()+"\"},"+
					//"{\"fieldName\":\"t100ry\",\"fieldValue\":\""+dt1.get(0).getUSERID()+"\"},"+
					"{\"fieldName\":\"gys\",\"fieldValue\":\""+dt1.get(0).getCUS_NO()+"\"},"+
					"{\"fieldName\":\"sxrq\",\"fieldValue\":\""+dt1.get(0).getSXRQ()+"\"},";

			if(null!=dt1.get(0).getJZRQ()){
				maindata+="{\"fieldName\":\"jzrq\",\"fieldValue\":\""+dt1.get(0).getJZRQ()+"\"},";
			}

			maindata+=
					"{\"fieldName\":\"hjdb\",\"fieldValue\":\""+dt1.get(0).getTYPENO()+"\"},"+
							"]";
			System.out.println(maindata);
			String detailData="[";
			detailData+="{\"tableDBName\":\"formtable_main_722_dt1\",\"workflowRequestTableRecords\":[";
			for (int i=0;i<dt1.size();i++){
				detailData+=
						// 每一行开始
						"{\"recordOrder\":0,\"workflowRequestTableFields\":["
								+ "{\"fieldName\":\"lh\",\"fieldValue\":\""+dt1.get(i).getLJBH()+"\"},"//料号
								+ "{\"fieldName\":\"dj\",\"fieldValue\":\""+dt1.get(i).getUP()+"\"},"//单价
								+ "{\"fieldName\":\"bz\",\"fieldValue\":\""+dt1.get(i).getREM()+"\"},"//备注
								+ "{\"fieldName\":\"zjyy\",\"fieldValue\":\""+dt1.get(i).getCAUSE()+"\"}"+//涨价原因
								"]},";

				// 每一行结束
			}

			detailData+="]}";
			detailData+="]";
			log.writeLog("maindata:"+maindata);
 			log.writeLog("detailData:"+detailData);
			log.writeLog("--------------------------------------------------------------------");

			String mmmm=detailData;
			int start=0;
			for(int ii=0;ii<mmmm.length();ii++){
				if(ii%800==0&&ii!=0){
					log.writeLog(mmmm.substring(start,ii));
					start=ii;
				}
			}
			if(start!=mmmm.length()){
				log.writeLog(mmmm.substring(start,mmmm.length()));
			}

			// 3. 创建参数列表，并进行参数编码
			List<NameValuePair> params = new ArrayList<>();
			params.add(new BasicNameValuePair("workflowId", dt1.get(0).getJUDGE3()));
			params.add(new BasicNameValuePair("requestName", "采购核价单-"+dt1.get(0).getJUDGE2()+"-"+dt1.get(0).getWLXQSJ()));
			params.add(new BasicNameValuePair("mainData",maindata));
			params.add(new BasicNameValuePair("detailData",detailData));

			params.add(new BasicNameValuePair("otherParams","{\"isnextflow\":\"0\"}"));
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");

			// 4. 将参数设置到HttpPost对象的Entity中
			httpPost.setEntity(entity);
			httpPost.setHeader("appid",FcHttpCon.APPID);
			httpPost.setHeader("token",token);
			httpPost.setHeader("userid",encryptUserid);

			// 5. 发送请求，并获取响应结果
			HttpResponse response = httpClient.execute(httpPost);
			String responseBody = EntityUtils.toString(response.getEntity());
			log.writeLog("responseBody:"+responseBody);

			System.out.println(responseBody);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


} 